package com.hanhai.material.scheduling.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.model.machine.MachineModel;
import com.hanhai.material.scheduling.entity.SysMachine;
import com.hanhai.material.scheduling.service.SysMachineService;
import com.hanhai.material.utils.redis.RedisUtil;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.hanhai.material.scheduling.entity.table.SysMachineTableDef.SYS_MACHINE;
import static com.mybatisflex.core.query.QueryMethods.distinct;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2023/10/18 11:26:03
 */
@RestController
@RequestMapping("/machine")
public class SysMachineController {


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private SysMachineService sysMachineService;

    @PostMapping("/list/{workShopName}")
    public Result<?> getMachineList(@PathVariable String workShopName){
//        WebSocketServer.sendData(MapBuilder.create().put("type","workShop").put("data",workShopName).build());
//        WebSocketServer.sendData(MapBuilder.create().put("type","status").put("data",true).build());
//        this.redisUtil.del("websocket");
//        WebSocketData webSocketData= new WebSocketData();
//        webSocketData.setWorkShop(workShopName);
//        webSocketData.setStatus(1);
//        webSocketData.setMaterialRecordList(new ArrayList<>());
//        webSocketData.setMachineName(null);
//        webSocketData.setWorkPlan(null);
//        this.redisUtil.set("websocket",webSocketData);
//        WebSocketServer.sendData(webSocketData);
        QueryWrapper queryWrapper=QueryWrapper.create();

        queryWrapper.select(distinct(SYS_MACHINE.MACHINE_AREA).as("name"),SYS_MACHINE.MACHINE_AREA.as("label"));
        queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
        queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
        queryWrapper.orderBy(SYS_MACHINE.MACHINE_AREA,true);
        List<MachineModel> machineList=this.sysMachineService.listAs(queryWrapper, MachineModel.class);
        if(machineList.isEmpty()){
            return ResultTool.fail();
        }
        int i=0;
        for(MachineModel machine:machineList){
            machine.setExtra(i);
            i++;
            queryWrapper=QueryWrapper.create();
            queryWrapper.select(distinct(SYS_MACHINE.MACHINE_NAME).as("name"),SYS_MACHINE.MACHINE_NAME.as("label"));
            queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
            queryWrapper.where(SYS_MACHINE.MACHINE_AREA.eq(machine.getName()));
            queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
            queryWrapper.orderBy(SYS_MACHINE.MACHINE_NAME,true);
            List<MachineModel> machineList1=this.sysMachineService.listAs(queryWrapper, MachineModel.class);
            for(int j=0;j<machineList1.size();j++){
                machineList1.get(j).setExtra(j);
            }
            machine.setChildren(machineList1);
        }
        return ResultTool.success(machineList);
    }

    @PostMapping("/list1/{workShopName}/{productDate}")
    public Result<?> getMachineList1(@PathVariable String workShopName, @PathVariable String productDate){
//        WebSocketServer.sendData(MapBuilder.create().put("type","workShop").put("data",workShopName).build());
//        WebSocketServer.sendData(MapBuilder.create().put("type","status").put("data",true).build());
//        this.redisUtil.del("websocket");
//        WebSocketData webSocketData= new WebSocketData();
//        webSocketData.setWorkShop(workShopName);
//        webSocketData.setStatus(1);
//        webSocketData.setMaterialRecordList(new ArrayList<>());
//        webSocketData.setMachineName(null);
//        webSocketData.setWorkPlan(null);
//        this.redisUtil.set("websocket",webSocketData);
//        WebSocketServer.sendData(webSocketData);
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.select(distinct(SYS_MACHINE.MACHINE_AREA).as("name"),SYS_MACHINE.MACHINE_AREA.as("label"));
        queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
        queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
        queryWrapper.orderBy(SYS_MACHINE.MACHINE_AREA,true);
        List<MachineModel> machineList=this.sysMachineService.listAs(queryWrapper, MachineModel.class);
        if(machineList.isEmpty()){
            return ResultTool.fail();
        }
        for(MachineModel machine:machineList){
            queryWrapper=QueryWrapper.create();
            queryWrapper.select(distinct(SYS_MACHINE.MACHINE_NAME).as("name"),SYS_MACHINE.MACHINE_NAME.as("label"));
            queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
            queryWrapper.where(SYS_MACHINE.MACHINE_AREA.eq(machine.getName()));
            queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));

            queryWrapper.orderBy(SYS_MACHINE.MACHINE_NAME,true);
            List<MachineModel> machineList1=this.sysMachineService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, MachineModel.class);

            machine.setChildren(machineList1);

        }
        return ResultTool.success(machineList);
    }

    @PostMapping("/reason/list/{workShopName}")
    public Result<?> getReasonMachineList(@PathVariable String workShopName){
//        WebSocketServer.sendData(MapBuilder.create().put("type","workShop").put("data",workShopName).build());
//        WebSocketServer.sendData(MapBuilder.create().put("type","status").put("data",true).build());
//        this.redisUtil.del("websocket");
//        WebSocketData webSocketData= new WebSocketData();
//        webSocketData.setWorkShop(workShopName);
//        webSocketData.setStatus(1);
//        webSocketData.setMaterialRecordList(new ArrayList<>());
//        webSocketData.setMachineName(null);
//        webSocketData.setWorkPlan(null);
//        this.redisUtil.set("websocket",webSocketData);
//        WebSocketServer.sendData(webSocketData);
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.select(distinct(SYS_MACHINE.MACHINE_AREA).as("name"),SYS_MACHINE.MACHINE_AREA.as("label"));
        queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
        queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
        queryWrapper.orderBy(SYS_MACHINE.MACHINE_AREA,true);
        List<MachineModel> machineList=this.sysMachineService.listAs(queryWrapper, MachineModel.class);
        if(machineList.isEmpty()){
            return ResultTool.fail();
        }
        for(MachineModel machine:machineList){
            queryWrapper=QueryWrapper.create();
            queryWrapper.select(distinct(SYS_MACHINE.MACHINE_NAME).as("name"),SYS_MACHINE.MACHINE_NAME.as("label"));
            queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
            queryWrapper.where(SYS_MACHINE.MACHINE_AREA.eq(machine.getName()));
            queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
            queryWrapper.orderBy(SYS_MACHINE.MACHINE_NAME,true);
            List<MachineModel> machineList1=this.sysMachineService.listAs(queryWrapper, MachineModel.class);
            machine.setChildren(machineList1);
        }
        return ResultTool.success(machineList);
    }



    @PostMapping("/listAll/{workShopName}")
    public Result<?> listAll(@PathVariable String workShopName){
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.select(distinct(SYS_MACHINE.MACHINE_NAME).as("name"),SYS_MACHINE.MACHINE_NAME.as("label"));
        queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShopName));
        queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(true));
        queryWrapper.orderBy(SYS_MACHINE.MACHINE_NAME,true);
        List<MachineModel> machineList1=this.sysMachineService.listAs(queryWrapper, MachineModel.class);

        return machineList1.isEmpty()?ResultTool.fail():ResultTool.success(queryWrapper);
    }
    @PostMapping("/list/{workShop}/{isMachine}")
    public Result<?> list(@PathVariable String workShop,@PathVariable boolean isMachine){
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.where(SYS_MACHINE.WORK_SHOP_NAME.eq(workShop));
        queryWrapper.where(SYS_MACHINE.IS_MACHINE.eq(isMachine));
        List<SysMachine> machineList=this.sysMachineService.list(queryWrapper);

        return machineList.isEmpty()?ResultTool.fail():ResultTool.success(machineList);
    }
}
